package com.party.controller.sys;

import com.party.common.MD5;
import com.party.controller.sys.sysManage.LeftMenusController;
import com.party.model.SysNav;
import com.party.model.SysUser;
import com.party.service.sys.MenuService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 后台主页面控制
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 13-2-15
 * Time: 下午11:52
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/sys")
public class MainController {
    @Resource
    MenuService menusService;
    @Resource
    LeftMenusController leftMenusController;
    @Resource
    SysUserService sysUserService;

    /**
     * 头部页面
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "header", method = RequestMethod.GET)
    public ModelAndView header(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        String passport = "";
        if ((SysUser) session.getAttribute("sysUser") != null) {
            passport = ((SysUser) session.getAttribute("sysUser")).getPassport();
        }

        List<SysNav> sysNavs = menusService.getNavsByPassport(passport);
        modelAndView.addObject("navs", sysNavs);

        modelAndView.setViewName("/sys/header");
        return modelAndView;
    }


    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView index(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/index");
        return modelAndView;
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public ModelAndView login(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/login");
        return modelAndView;
    }


    @RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
    public ModelAndView loginCheck(
            @RequestParam("userName") String userName,
            @RequestParam("pwd") String pwd,
            @RequestParam(value = "code") String code,
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session
    ) {
        int status = 0;

        if (code != null) {
            String rand = (String) session.getAttribute("rand");
            if (code.equals(rand)) {
                status = 1;
            } else {
                status = -2;
            }
        } else {
            status = -2;
        }

        if (status == 1) {
            SysUser sysUser = new SysUser();
            sysUser.setPassport(userName);
            sysUser = sysUserService.getUser(sysUser);
            if (sysUser != null) {
                if (sysUser.getPwd().equals(MD5.encodeMD5(pwd))) {
//                if (sysUser.getPwd().equals(pwd)) {
                    status = 1;
                    session.setAttribute("sysUser", sysUser);
                } else {
                    status = -1;
                }
            }
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", status);
        modelAndView.setViewName("/sys/login");
        return modelAndView;
    }


    @RequestMapping(value = "/loginOut", method = RequestMethod.GET)
    public ModelAndView loginOut(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session
    ) {
        //清除session
        session.removeAttribute("sysUser");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("out", 1);
        modelAndView.setViewName("/sys/login");

        return modelAndView;
    }

    /**
     * 获取左侧菜单栏
     *
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "left")
    public ModelAndView left(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        String passport = "";
        if ((SysUser) session.getAttribute("sysUser") != null) {
            passport = ((SysUser) session.getAttribute("sysUser")).getPassport();
        }
        List<SysNav> tNavs = menusService.getNavsByPassport(passport);
        return leftMenusController.left(tNavs.get(0).getNavid().intValue() + "", session, request, response);
    }

    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String main(HttpSession session) {
        return "/sys/main";
    }
}
